<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-item">
        <span class="span">任务名称 </span>
        <el-input v-model="listQuery.jobName" placeholder="请输入任务名称" style="width: 200px" />
      </div>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        {{ $t('table.search') }}
      </el-button>
      <el-button v-waves v-permission="['dataextraction:DataExtractionTask:add']" class="filter-item" style="margin-left: 10px" type="primary" icon="el-icon-edit" @click="handleCreate">
        {{ $t('table.add') }}
      </el-button>
    </div>

    <el-table ref="table" :key="tableKey" v-loading="listLoading" element-loading-text="正在加载，请稍后" :data="list" border fit highlight-current-row style="width: 100%">
      <el-table-column type="index" align="center" width="100" label="序号" />
      <el-table-column label="任务名称" align="center" min-width="150px">
        <template slot-scope="{ row }">
          <span>{{ row.jobName }}</span>
        </template>
      </el-table-column>

      <el-table-column label="任务状态" align="center" min-width="150px">
        <template slot-scope="{ row }">
          <span>{{ row.checkStatus | dictLabel('is_use') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="cron表达式" align="center" min-width="150px">
        <template slot-scope="{ row }">
          <span>{{ row.cronExpression }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" fixed="right" align="center" min-width="230" class-name="small-padding fixed-width">
        <template slot-scope="{ row }">
          <el-button v-if="row.checkStatus == 0" type="text" size="small" icon="el-icon-video-play" style="margin-left: 10px" @click="handleChangeJobStatus(row, '1', '启动')">开始</el-button>
          <el-button v-if="row.checkStatus == 1" type="text" size="small" icon="el-icon-video-pause" class="delete-text-btn" style="margin-left: 10px" @click="handleChangeJobStatus(row, '0', '停止')"
            >停止</el-button
          >
          <el-button size="small" type="text" icon="el-icon-video-play" @click="runAJobNow(row)">执行一次</el-button>
          <el-button icon="el-icon-document" size="small" type="text" @click="handleLook(row)"> {{ $t('table.look') }}</el-button>
          <el-button v-permission="['dataextraction:DataExtractionTask:update']" type="text" size="small" icon="el-icon-edit" @click="handleUpdate(row)">
            {{ $t('table.edit') }}
          </el-button>
          <el-button v-permission="['dataextraction:DataExtractionTask:delete']" type="text" size="small" icon="el-icon-delete" class="delete-text-btn" @click="handleDelete(row)">
            {{ $t('table.delete') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" :page-sizes="pageArray" @pagination="getList" />

    <data-extraction-task-form ref="form" @refreshList="getList" />
  </div>
</template>

<script>
import { deleteDataExtractionTask, fetchDataExtractionTaskList, changeJobStatus, startOne } from '@/api/dataextractiontask/dataExtractionTask'
import waves from '@/directive/waves' // waves directive
import permission from '@/directive/permission/permission'
import dataExtractionTaskForm from './dataExtractionTaskForm'
import Pagination from '@/components/Pagination'

export default {
  name: 'DataExtractionTaskList',
  components: { dataExtractionTaskForm, Pagination },
  directives: { waves, permission },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      pageArray: this.$store.getters.pageArray,
      listQuery: {
        jobName: undefined,
        checkStatus: undefined,
        page: 1,
        limit: this.$store.getters.defaultPageSize,
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchDataExtractionTaskList(this.listQuery).then((response) => {
        this.list = response.data.data
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleCreate() {
      this.$refs.form.handleCreate()
    },
    handleUpdate(row) {
      this.$refs.form.handleUpdate(row.id)
    },
    handleDelete(row) {
      this.$confirm('删除任务会级联删除任务日志，是否确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        deleteDataExtractionTask(row.id).then((response) => {
          if (response.data.code === 0) {
            this.getList()
            this.$message.success(response.data.msg)
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    },
    handleChangeJobStatus(row, cmd, cmdLabel) {
      this.$confirm('你确定要' + cmdLabel + '该计划任务么?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        let obj = {
          jobName: row.jobName,
          id: row.id,
          checkStatus: cmd,
        }
        changeJobStatus(obj).then((response) => {
          const data = response.data
          if (data.code === 0) {
            this.$message.success(data.msg)
            this.getList()
          } else {
            this.$message.error(data.errmsg)
          }
        })
      })
    },
    runAJobNow(row) {
      startOne({ id: row.id }).then((res) => {
        if (res.data.code == 0) {
          this.$message.success(res.data.msg)
          this.getList()
        } else {
          this.$message.error(res.data.msg)
        }
      })
    },
    handleLook(row) {
      this.$refs.form.handleLook(row.id)
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
  },
}
</script>
<style lang="scss" scoped>
::v-deep(::-webkit-scrollbar) {
  display: block !important;
}
.span {
  color: #606266;
  font-weight: 700;
  font-size: 14px;
}
</style>
